//
//  SampuxHourPickerView.m
//  Sampux
//
//  Created by rzm on 16/9/11.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxHourPickerView.h"


@interface SampuxHourPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, assign) NSInteger selectedHourIndex ;
@property (nonatomic, assign) NSInteger selectedMinIndex ;

@end
@implementation SampuxHourPickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.delegate = self;
        self.dataSource = self;
        [self setValue:THWhiteAlplaColor(0.8) forKey:@"textColor"];
    }
    return self;
}


#pragma mark UIPickerViewDetaSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return HourArr.count *50;
    }else
    {
        return MinArr.count *50;
    }
}

#pragma mark UIPickerViewDelegate
/**
 *  pickerView每行的视图
 */
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
   
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.minimumScaleFactor = 12.0;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:20]];
        pickerLabel.textColor = THWhiteAlplaColor(0.8);
         pickerLabel.frame = CGRectMake(12.0f, 0.0f, [pickerView rowSizeForComponent:component].width-12, [pickerView rowSizeForComponent:component].height);
    }
    ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = THWhiteAlplaColor(0.8);
    ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = THWhiteAlplaColor(0.8);
    // Fill the label text here
    if (component == 0) {
        pickerLabel.text = [HourArr objectAtIndex:(row%[HourArr count])];

    }else
    {
        pickerLabel.text = [MinArr objectAtIndex:(row%[MinArr count])];
    }
    return pickerLabel;
}

/**
 *  轮子显示内容
 */
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        return [HourArr objectAtIndex:(row%[HourArr count])];
    }else
    {
        return [MinArr objectAtIndex:(row%[MinArr count])];
    }
}

/**
 *  轮子的移动
 // */
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSUInteger max = 0;
    NSUInteger base10 = 0;
    if(component == 0)
    {
        max = [HourArr count]*50;
        base10 = (max/2)-(max/2)%[HourArr count];
        [pickerView selectRow:[pickerView selectedRowInComponent:component]%[HourArr count]+base10 inComponent:component animated:false];
        self.selectedHourIndex = [pickerView selectedRowInComponent:0]%[HourArr count];
        self.hourStr = HourArr[self.selectedHourIndex];
    }else
    {
        
        
        max = [HourArr count]*50;
        base10 = (max/2)-(max/2)%[HourArr count];
        [pickerView selectRow:[pickerView selectedRowInComponent:component]%[MinArr count]+base10 inComponent:component animated:false];

        self.selectedMinIndex = [pickerView selectedRowInComponent:1]%[MinArr count];
        self.minStr = MinArr[self.selectedMinIndex];
    }
    
    if ([self.installDelegate respondsToSelector:@selector(changeHourAndMin:andminStr:)]) {
        [self.installDelegate changeHourAndMin:self.hourStr andminStr:self.minStr];
    }
}

/**
 *  pickerView的行高
 */
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return self.height/3;
}

- (void)setSelectedHourIndex:(NSInteger)selectedHourIndex
{
    _selectedHourIndex = selectedHourIndex;
}

- (void)setSelectedMinIndex:(NSInteger)selectedMinIndex
{
    _selectedMinIndex = selectedMinIndex;
}

- (void)setHourStr:(NSString *)hourStr
{
    _hourStr = hourStr;
}
- (void)setMinStr:(NSString *)minStr
{
    _minStr = minStr;
}

- (void)setPickerInitialRow:(NSInteger)pickerInitialRow
{
    _pickerInitialRow = pickerInitialRow + [HourArr count]*25;
    [self selectRow:_pickerInitialRow inComponent:0 animated:YES];
    
}
- (void)setPickerMinInitialRow:(NSInteger)pickerMinInitialRow
{
    _pickerMinInitialRow = pickerMinInitialRow + [MinArr count]*25;
    [self selectRow:_pickerMinInitialRow inComponent:1 animated:YES];
    
}
@end
